﻿using SQLite;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using TabbedTemplate.Models;

namespace TabbedTemplate.Services
{
    public class PunchInfoStorage : IPunchInfoStorage
    {

        private IPreferenceStorage _preferenceStorage;

        public PunchInfoStorage(IPreferenceStorage preferenceStorage)
        {
            _preferenceStorage = preferenceStorage;
        }

        // ******** 公开常量
        /// <summary>
        /// 打卡记录数据库名。
        /// </summary>
        public const string DbName = "punchInfoStorage.sqlite3";
        // ******** 私有变量
        /// <summary>
        /// 搜索内容数据库路径。
        /// </summary>
        /// <returns></returns>

        public static readonly string PunchInfoDbPath = Path.Combine(
            Environment.GetFolderPath(Environment.SpecialFolder
                .LocalApplicationData), DbName);
        /// <summary>
        /// 数据库连接影子变量。
        /// </summary>
        private SQLiteAsyncConnection _connection;

        /// <summary>
        /// 数据库连接。
        /// </summary>
        private SQLiteAsyncConnection Connection =>
            _connection ??
            (_connection = new SQLiteAsyncConnection(PunchInfoDbPath));

        public bool PunchInfoStorageInitialized() => _preferenceStorage.Get(PunchInfoStorageConstants.VersionKey, -1) ==
            PunchInfoStorageConstants.Version;

        public async Task PunchInfoStorageInitializeAsync()
        {
            await Connection.CreateTableAsync<PunchInfo>();
            _preferenceStorage.Set(PunchInfoStorageConstants.VersionKey, PunchInfoStorageConstants.Version);
        }

        public async Task DeleteTableAsync()
        {
            await Connection.DropTableAsync<PunchInfo>();
            _preferenceStorage.Set(PunchInfoStorageConstants.VersionKey, 100);
        }

       
        public async Task<long> InsertPunchInfoAsync(string storeUid,
            string content)
        {
            long longToReturn = DateTime.Now.Ticks;
            await Connection.InsertAsync(new PunchInfo
            {
                Store_Uid = storeUid,
                Content = content,
                Timestamp = longToReturn
            });
            return longToReturn;
        }


        public async Task SavePunchInfoAsync(PunchInfo punchInfo, bool generateTimestamp)
        {

            if (generateTimestamp)
            {
                var longToReturn = DateTime.Now.Ticks;
                punchInfo.Timestamp = longToReturn;
            }

            await Connection.InsertOrReplaceAsync(punchInfo);

        }
        public async Task InsertAllPunchInfoAsync(IList<PunchInfo> punchInfoList)
        {
            await Connection.InsertAllAsync(punchInfoList);
        }
        public async Task DeletePunchInfoByIdAsync(PunchInfo punchInfo) =>
            await Connection.DeleteAsync(punchInfo);

        public async Task<PunchInfo> GetPunchInfoById(int Id) =>
            await Connection.Table<PunchInfo>()
            .FirstOrDefaultAsync(p => p.Id == Id);

        public async Task<IList<PunchInfo>> GetAllPunchInfoByStoreUid(string storeUid) =>
             await Connection.Table<PunchInfo>()
                .Where(p => p.Store_Uid == storeUid).ToListAsync();


        public async Task<IList<PunchInfo>> GetAllPunchInfosAsync() =>
            await Connection.Table<PunchInfo>().OrderByDescending<long>(p => p.Timestamp).ToListAsync();
        public async Task<IList<PunchInfo>> GetAllPunchInfosAsync(int skip, int take) =>
            await Connection.Table<PunchInfo>().OrderByDescending<long>(p => p.Timestamp).Skip(skip).Take(take).ToListAsync();
    }
    public static class PunchInfoStorageConstants
    {
        /// <summary>
        /// 版本。
        /// </summary>
        public const string VersionKey =
            nameof(IPunchInfoStorage) + "." + nameof(Version);

        /// <summary>
        /// 版本。
        /// </summary>
        public const int Version = 1;
    }
}
